package com.yumeng.framework.persistence.mybatis.helper;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.yumeng.common.data.algorithm.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wxd
 * @date 2021/12/9 16:10
 */
@Slf4j
public class CustomIdGenerator implements IdentifierGenerator {

    public final SnowflakeIdWorker idWorker;

    public CustomIdGenerator(long workerId, long datacenterId){
        idWorker = new SnowflakeIdWorker(workerId, datacenterId);
    }

    /**
     * 生成Id
     *
     * @param entity 实体
     * @return id
     */
    @Override
    public Number nextId(Object entity) {
        //可以将当前传入的class全类名来作为bizKey,或者提取参数来生成bizKey进行分布式Id调用生成.
/*        String bizKey = entity.getClass().getName();
        log.debug("bizKey:{}", bizKey);
        long id = idWorker.nextId();
        log.debug("生成主键值->:{}", id);
        return id;*/

        return idWorker.nextId();

    }

    /**
     * 生成uuid
     *
     * @param entity 实体
     * @return uuid
     */
    @Override
    public String nextUUID(Object entity) {
        return IdentifierGenerator.super.nextUUID(entity);
    }
}
